@echo off
@echo DWARFS SETUP
@echo ===========
@echo Please run this script (Setup.bat) with elevated rights.
@echo.
@echo.
@echo The setup consists of five steps: 
@echo  1.) Setting environment variables.
@echo  2.) Ensuring that the registry key for the DwarfBench cmdlet has been removed.
@echo  3.) Build the dwarfs and dwarf prerequisites (sets some additional environment variables).
@echo  4.) Creating a network share to allow dwarf execution on a HPC cluster.
@echo  5.) Generating input for each dwarf with pre-defined input sizes "small", "medium" and "large".
@echo  6.) Locating and configuring DwarfBench for Excel and Java support.
@echo.
@echo.
@echo Dwarfs require the following components:
@echo. 
@echo  1.) Visual Studio 2010
@echo  2.) PowerShell (v1.0)
@echo  3.) MSMPI, MPI.NET (v1.0)
@echo  4.) Xperf/XperfView
@echo  5.) JumpShot and/or Vampir. 
@echo  6.) Microsoft Excel 2007
@echo  7.) Microsoft HPC Pack 2008 (optional), Microsoft HPC Pack SDK
@echo  8.) Supported operating system: Microsoft Windows Vista, Windows Server 2008, Windows7
@echo.
@echo Please refer to Dwarfs documentation for download information.
@echo Please note that the absence of one or more of these components or their improper configuration can lead to DwarfBench instability.
@echo Logging information during the setup process will be written to .\Logs\SetupLog.txt
@echo.
@echo Would you like to continue with setup?
CHOICE /C YN /M "Press Y for Yes, N for No."
if NOT %ERRORLEVEL%==1 goto end
@echo.
REM @echo.
REM @echo Checking for components...
REM call %DWARF_DIR%Bin\Scripts\InputFileGenerator\TestComponents.bat

@echo.
@echo STEP 1: 
@echo =======
@echo Setting up initial dwarf environment variables...
@echo.

set DWARF_DIR=%~dp0
set SETUP_OUT_F="%DWARF_DIR%Logs\SetupLog.txt"
set SETUP_OUT_BF="%DWARF_DIR%Logs\BuildLog.txt"
echo Logging setup output > %SETUP_OUT_F%
echo Build output > %SETUP_OUT_BF%
date /t 1>>%SETUP_OUT_F% 2>&1 
date /t 1>>%SETUP_OUT_BF% 2>&1

FOR /F "skip=2 tokens=3" %%i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\net framework setup\ndp\v4\client" /v version') DO set FRAMEWORK=%%i

@echo.
@echo STEP 2: 
@echo =======
@echo Ensuring the DwarfBench cmdlet registry key is not present.
@echo.

@echo Trying to remove the DwarfBench PowerShell snap-in key from the registry.
reg DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\DwarfPsh /f > %SETUP_OUT_f%
@echo.

:skipsetenv

@echo STEP 3: 
@echo =======
@echo Would you like to build the dwarfs now?
CHOICE /C YN /M "Press Y for Yes, N for No."
if NOT %ERRORLEVEL%==1 goto skipbuild

@echo Building the prerequisites...
@echo.
%windir%\Microsoft.NET\Framework\v%FRAMEWORK%\MSBuild.exe /t:Build /p:Configuration=Release;TargetFrameworkVersion=v%FRAMEWORK% "%DWARF_DIR%Bin\Scripts\Lib\SetupTool\SetupTool.sln" 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
%windir%\Microsoft.NET\Framework\v%FRAMEWORK%\MSBuild.exe /t:Build /p:Configuration=Release;TargetFrameworkVersion=v%FRAMEWORK% "%DWARF_DIR%Bin\Scripts\Lib\SetEnv\SetEnv.sln" 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
REM ==========================

@echo Setting some additional environment variables...
@echo Setting: DWARF_DIR=%~dp0
"%DWARF_DIR%DwarfBench\PshCliWrapper\SetEnv.exe" DWARF_DIR "%~dp0

@echo Setting: DWARF_PSH_DIR=%~dp0DwarfBench\PshCliWrapper
"%DWARF_DIR%DwarfBench\PshCliWrapper\SetEnv.exe" DWARF_PSH_DIR "%~dp0DwarfBench\PshCliWrapper" 
set DWARF_PSH_DIR=%~dp0DwarfBench\PshCliWrapper

REM @echo Setting: DWARF_BENCH %~dp0DwarfBench
REM "%DWARF_DIR%DwarfBench\PshCliWrapper\SetEnv.exe" DWARF_BENCH "%~dp0DwarfBench" 

echo. 1>>%SETUP_OUT_F% 2>&1 
@echo.

@echo Building kernels...
@echo Builing the kernels 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
%windir%\Microsoft.NET\Framework\v%FRAMEWORK%\MSBuild.exe /t:Build /p:Configuration=Release;TargetFrameworkVersion=v%FRAMEWORK% "%DWARF_DIR%Dwarfs.sln" 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
@echo. 1>>%SETUP_OUT_BF% 2>&1 
@echo Building DwarfBench
%windir%\Microsoft.NET\Framework\v3.5\MSBuild.exe /t:Build /p:Configuration=Release;TargetFrameworkVersion=v3.5 "%DWARF_DIR%DwarfBench\PshCli\PshCli.sln" 1>>%SETUP_OUT_BF% 2>&1 

@echo Copying CliTools.dll and verifying execution directory structure
call "%DWARF_DIR%Bin\Scripts\InputFileGenerator\DistributeCliTools.bat" 1>>%SETUP_OUT_F% 2>&1 
@echo.

findstr /I "FAILED" %SETUP_OUT_BF%

@echo Build process complteted. See \Logs\BuildLog.txt for results.

@echo.
@echo.
:skipbuild
@echo STEP 4: 
@echo =======
@echo Running the dwarfs on a cluster requires a network share on the local machine to be used during deployment of binaries (e.g. kernel executables). Would you like to share Dwarfs\Bin?
CHOICE /C YN /M "Press Y for Yes, N for No."
if NOT %ERRORLEVEL%==1 goto skipsetshare
goto setshare

:setshare
echo.
REM trying to delete old/pre-set dwarf share.
net share DWARF_SHARE_%username% /delete 1>>%SETUP_OUT_F% 2>&1 
@echo Creating network share: DWARF_SHARE_%username%=%DWARF_DIR%Bin
net share DWARF_SHARE_%username%="%DWARF_DIR%Bin" /GRANT:Everyone,FULL 1>>%SETUP_OUT_F% 2>&1 
setx DWARF_SHARE_DIR \\%computername%\DWARF_SHARE_%username%\ 1>>%SETUP_OUT_F% 2>&1 
set DWARF_SHARE_DIR=\\%computername%\DWARF_SHARE_%username%\ 1>>%SETUP_OUT_F% 2>&1 

:skipsetshare
@echo.
@echo.
@echo STEP 5: 
@echo =======
@echo Input files are required for execution of dwarf kernels. Would you like to generate input now?
@echo Please note, this step can take up tp 30 minutes depending on the performance of your system. "Small", "medium" and "large" inputs are generated for each dwarf.
echo.
@echo You can generate input manually by using the batch script .\Bin\Scripts\InputFileGenerator. Please refer to the documentation for detailed information of input generation.
CHOICE /C YN /M "Press Y for Yes, N for No."
if NOT %ERRORLEVEL%==1 goto skipgenerateinput
@echo.
@echo Would you like to generate the input files asynchronously?
CHOICE /C YN /M "Press Y for Yes, N for No."
if NOT %ERRORLEVEL%==1 goto serialinput
call "%DWARF_DIR%Bin\Scripts\InputFileGenerator\ParallelInputFileGeneratorCreateInput.bat"
goto skipgenerateinput
:serialinput
call "%DWARF_DIR%Bin\Scripts\InputFileGenerator\InputFileGeneratorCreateInput.bat"

:skipgenerateinput
@echo.
REM Creating AddIn registration file
call %DWARF_DIR%Bin\Scripts\InputFileGenerator\InstallVSAddIn.bat 1>>%SETUP_OUT_F% 2>&1 
REM @echo AddIn installation complete.
REM @echo.
REM Enabling DwarfBench PowerShell commandlet.
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -nologo "set-executionpolicy unrestricted" 1>>%SETUP_OUT_F% 2>&1 
@echo STEP 6:
@echo ======= 
@echo Locating and configuring DwarfBench for Excel and Java support
@echo.
"%DWARF_DIR%DwarfBench\PshCliWrapper\SetupTool.exe" "%DWARF_DIR%dwarfconfig.xml" 1>>%SETUP_OUT_F% 2>&1 
@echo.
@echo Note: If you intend to use JumpShot for mpi trace analysis, please enter the full jumpshot path as value into .\dwarfconfig.xml.
@echo For example "<JumpShotPath>C:\Users\myusername\Desktop\slog2rte\slog2rte-1.2.6\lib\</JumpShotPath>"
@echo Done.
@echo.
@echo Setup complete!
@echo Launching PowerShell with DwarfBench cmdlet...
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -nologo -command "& $env:DWARF_DIR\DwarfBench\PshCliWrapper\DwarfInitCli.ps1"
REM @echo You can now start running DwarfBench. Please run StartDwarfBench from the Dwarf root directory.

:end